package com.extension.company.view.my.collect;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.TextView;

import com.extension.company.admin.R;
import com.extension.company.common.mvp.BaseTitleActivity;
import com.extension.company.contract.MyCollectionContrtact;
import com.extension.company.presenter.my.MyCollectionPresenter;

import java.util.List;

import butterknife.BindViews;

/**
 * 我的收藏
 */
public class MyCollectionActivity extends BaseTitleActivity<MyCollectionContrtact.View, MyCollectionPresenter>
		implements MyCollectionContrtact.View, View.OnClickListener {

	/**
	 * tabs 标签列表
	 */
	@BindViews({R.id.tv_tag1, R.id.tv_tag2})
	List<TextView> tags;

	/**
	 * 最近
	 */
	private MyRecentlyCollectFragment myRecentlyCollectFragment;
	/**
	 * 佣金
	 */
	private MyCommissionFragment myCommissionFragment;

	/**
	 * fragment 管理者
	 */
	private FragmentManager fragmentManager;

	/**
	 * 当前展示的Fragment
	 */
	private Fragment mContent;

	public static void startActivity(Activity activity) {
		Intent intent = new Intent(activity, MyCollectionActivity.class);
		activity.startActivity(intent);
	}

	@Override
	protected void initData() {
		setTitle("我的收藏");
		myRecentlyCollectFragment = new MyRecentlyCollectFragment();  // 最近Fragment
		myCommissionFragment = new MyCommissionFragment(); // 佣金Fragment
		fragmentManager = getSupportFragmentManager();
		fragmentManager.beginTransaction()
				.replace(R.id.fm_View, myRecentlyCollectFragment)
				.commit();

		mContent = myRecentlyCollectFragment;
	}

	@Override
	protected void initListener() {
		tags.get(0).setOnClickListener(this); // 最近tag 监听
		tags.get(1).setOnClickListener(this); // 佣金tag 监听
	}

	@Override
	public int getMainLayout() {
		return R.layout.mycollection;
	}

	@Override
	public MyCollectionContrtact.View createView() {
		return this;
	}

	@Override
	public MyCollectionPresenter createPresenter() {
		return new MyCollectionPresenter();
	}

	@Override
	public void showLoading() {

	}

	@Override
	public void hideLoading() {

	}

	@Override
	public void onError(Throwable throwable) {

	}

	@Override
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.tv_tag1:
				setPagerStatus(0);
				break;
			case R.id.tv_tag2:
				setPagerStatus(1);
				break;
		}
	}

	/**
	 * 功能：切换fragment
	 *
	 * @param from 当前的fragment
	 * @param to   要跳转的fragment
	 */
	private void switchContent(Fragment from, Fragment to) {
		if (mContent != to) {
			mContent = to;  //记住当前的fragment
			FragmentTransaction transaction = fragmentManager.beginTransaction();
			// 先判断是否被add过
			if (!to.isAdded()) {
				// 隐藏当前的fragment，add下一个到Activity中
				transaction.hide(from).add(R.id.fm_View, to).commit();
			} else {
				// 隐藏当前的fragment，显示下一个
				transaction.hide(from).show(to).commit();
			}
		}
	}

	/**
	 * 点击标签切换fragment、设置相关状态
	 *
	 * @param temp 标签
	 */
	private void setPagerStatus(int temp) {
		tags.get(0).setTextColor(getResources().getColor(R.color.tvc23));
		tags.get(1).setTextColor(getResources().getColor(R.color.tvc23)); // 这两句是重置tag颜色
		if (temp == 0) {
			tags.get(0).setTextColor(getResources().getColor(R.color.topcolor)); // 更改点击的颜色
			switchContent(mContent, myRecentlyCollectFragment); // 更换相应的fragment
		} else if (temp == 1) {
			tags.get(1).setTextColor(getResources().getColor(R.color.topcolor));
			switchContent(mContent, myCommissionFragment);
		}
	}
}
